﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PTA
{
    public class ContextMessage
    {
        #region 内部成员
        private const string MessageName = "PTA_CONTEXT_MESSAGE_NAME";
        private static ContextMessage _curmsg = new ContextMessage();
        protected static ContextMessage curmsg
        {
            get
            {
                if (_curmsg == null)
                {
                    _curmsg = new ContextMessage();
                }
                return _curmsg;
            }
        }
        private bool success = true;
        private List<string> msgs = null;
        private string sessionId = string.Empty;
        private int userId;
        #endregion

        public bool Success
        {
            get
            {
                return this.success;
            }
            set
            {
                this.success = value;
            }
        }
        public bool HasMessage
        {
            get
            {
                return this.msgs != null && this.msgs.Count > 0;
            }
        }
        public List<string> MessageList
        {
            get
            {
                return this.msgs;
            }
        }
        public List<string> Add(string msg)
        {
            if (this.msgs == null)
            {
                this.msgs = new List<string>();
            }
            this.msgs.Add(msg);
            return this.msgs;
        }
        public void Clear()
        {
            if (this.msgs != null)
            {
                this.msgs.Clear();
            }
        }
        public int UserId { get { return this.userId; } }
        public string SessionId { get { return this.sessionId; } }
        public override string ToString()
        {
            if (this.msgs == null || this.msgs.Count == 0)
            {
                return string.Empty;
            }
            return PTA.Convert.ToStrictString(this.msgs);
        }
        public static ContextMessage Current
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                {
                    if (!System.Web.HttpContext.Current.Items.Contains(MessageName))
                    {
                        var msg = new ContextMessage();
                        if (System.Web.HttpContext.Current.Session != null)
                        {
                            msg.sessionId = System.Web.HttpContext.Current.Session.SessionID;
                            if(PTA.Web.WebContext.CurrentUser !=null )
                            {
                                msg.userId = PTA.Web.WebContext.CurrentUser.UserId;
                            }
                        }
                        System.Web.HttpContext.Current.Items.Add(MessageName, msg);
                    }
                    return (ContextMessage)System.Web.HttpContext.Current.Items[MessageName];
                }
                else
                {
                    return curmsg;
                }
            }
        }
    }
}